﻿
namespace HSS.ComponentModel
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Globalization;
	using System.Windows;
	#endregion

	#region ThicknessConverter
	/// <summary>
	/// Converts instances of other types to and from instances of <see cref="Thickness"/>.
	/// </summary>
	public class ThicknessConverter : TypeConverter
	{
		/// <summary>
		/// Determines whether the type converter can create an instance of Thickness from a specified type.
		/// </summary>
		/// <param name="context">The context information of a type.</param>
		/// <param name="sourceType">The source type that the type converter is evaluating for conversion.</param>
		/// <returns>true if the type converter can create an instance of Thickness from the specified type; otherwise, false.</returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}
		/// <summary>
		/// Attempts to create an instance of Thickness from a specified object.
		/// </summary>
		/// <param name="context">The context information for a type.</param>
		/// <param name="culture">The CultureInfo of the type being converted.</param>
		/// <param name="value">The <i>source</i> Object being converted.</param>
		/// <returns>An instance of <see cref="Thickness"/> created from the converted <i>source</i>.</returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (value is string)
			{
				string s = ((string)value).Trim();
				return ParseThickness(s);
			}
			return base.ConvertFrom(context, culture, value);
		}
		/// <summary>
		/// Determines whether the type converter can convert an instance of Thickness to a different type. 
		/// </summary>
		/// <param name="context">The context information of a type.</param>
		/// <param name="destinationType">The type for which the type converter is evaluating this instance of Thickness for conversion.</param>
		/// <returns>true if the type converter can convert this instance of Thickness to the destinationType; otherwise, false.</returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
		}
		/// <summary>
		/// Attempts to convert an instance of Thickness to a specified type.
		/// </summary>
		/// <param name="context">The context information of a type.</param>
		/// <param name="culture">The CultureInfo of the type being converted.</param>
		/// <param name="value">The instance of Thickness to convert.</param>
		/// <param name="destinationType">The type that this instance of Thickness is converted to.</param>
		/// <returns>The type that is created when the type converter converts an instance of Thickness.</returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				if (value is Thickness)
					return ((Thickness)value).ToString();
			}
			if (destinationType == typeof(Thickness))
			{
				if (value is Thickness)
					return value;
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

		private static Thickness ParseThickness(string text)
		{
			string[] strArray = text.Split(new char[] { ',' });
			double[] numArray = new double[strArray.Length];
			for (int i = 0; i < strArray.Length; i++)
			{
				numArray[i] = double.Parse(strArray[i], NumberStyles.Float, CultureInfo.InvariantCulture);
			}
			if (((numArray.Length == 3) || (numArray.Length > 4)) || (numArray.Length == 0))
			{
				throw new FormatException("Must specify 1, 2, or 4 groups of digits");
			}
			if (numArray.Length == 1)
			{
				return new Thickness(numArray[0]);
			}
			if (numArray.Length == 2)
			{
				return new Thickness(numArray[0], numArray[1], numArray[0], numArray[1]);
			}
			return new Thickness(numArray[0], numArray[1], numArray[2], numArray[3]);
		}

	}
	#endregion
}